package com.jy.adscm.dealer.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.dealer.bean.DealerMembers;
import com.jy.adscm.dealer.mapper.DealerMembersMapper;
import com.jy.adscm.dealer.service.DealerInfoService;
import com.jy.adscm.dealer.service.DealerMembersService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.utils.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-01-16
 */
@Service
public class DealerMembersServiceImpl extends ServiceImpl<DealerMembersMapper, DealerMembers> implements DealerMembersService {
     @Autowired
    DealerInfoService dealerInfoService;
     @Transactional
    @Override
    public boolean insertDealerMembers(DealerMembers dealerMembers, Token token) throws AdscmSystemException {
         dealerMembers.setCreateTime(new Date());
         dealerMembers.setCreator(token.getUser().getId().toString());
        boolean insert = super.insert(dealerMembers);
        dealerInfoService.insertDealerInfo(dealerMembers,token,DealerMembers.class,dealerMembers.getDid());
        return insert;
    }
    @Transactional
    @Override
    public boolean deleteDealerMembers(Integer id, Token token) throws AdscmSystemException {
        DealerMembers dealerMembers = super.selectById(id);
        boolean b = super.deleteById(id);
        dealerInfoService.deleteDealerInfo(dealerMembers,token,DealerMembers.class,dealerMembers.getDid());
        return b;
    }
    @Transactional
    @Override
    public Boolean updateDealerMembers(DealerMembers dealerMembers, Token token) throws AdscmSystemException {
        DealerMembers olddealerMembers = super.selectById(dealerMembers.getDid());
        boolean b = super.updateById(dealerMembers);
        dealerInfoService.updateDealerInfo(dealerMembers,olddealerMembers,token,DealerMembers.class,dealerMembers.getDid());
        return b;
    }

    @Override
    public List<DealerMembers> selectDealerMembers(Integer did) throws AdscmSystemException {
        Wrapper<DealerMembers> wrapper =new EntityWrapper<>();
        wrapper.eq("did",did);
        List<DealerMembers> dealerMembers = super.selectList(wrapper);
        return dealerMembers;
    }
}
